package com.by.yz.mypersonal;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.afollestad.materialdialogs.MaterialDialog;
import com.by.yz.R;
import com.by.yz.activity.LoginActivity;
import com.by.yz.activity.ResetPasswordActivity;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.activity.regitser.RegisterInfoActivity;
import com.by.yz.entity.Account;
import com.by.yz.entity.Password;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.PasswordRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.loginUtil.codeRESTFul;
import com.by.yz.util.ToJSonObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by mrs yang on 2017/1/17.
 */
public class RevisePassword extends BaseActivity{
    private EditText phone;
    private String CODE = "";
    private EditText register_vercode;//填写验证码
    private Button getCode;//获取验证码
    private Button next;
    private String phoneNumber;
    String myPhone="";
    Account account;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.revisepwd);
        myPhone = getIntent().getStringExtra("myPhone");
        Log.e("myPhone1 = ",myPhone);
        phone = (EditText) findViewById(R.id.phonenum);
        if(!myPhone.equals("")) {
            phone.setText(myPhone);
            phone.setEnabled(false);
            phone.setFocusable(false);
        }
        register_vercode = (EditText) findViewById(R.id.vercode);//填写验证码
        getCode = (Button) findViewById(R.id.forget_getCode);//获取验证码
        next = (Button) findViewById(R.id.forget_finish);//完成

        final timeCount timecount = new timeCount(60000, 1000);
        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phoneNumber=phone.getText().toString();
                Log.e("ggg",String.valueOf(phoneNumber.length()));
                if(phoneNumber.length()!=11){
                    new MaterialDialog.Builder(RevisePassword.this).content("手机号码非法,请重新输入").positiveText("确定").show();
                }else{
                    AccountRESTFul accountRESTFul=new AccountRESTFul();
                    accountRESTFul.get(new AccountRESTFul.IhttpCb() {
                        @Override
                        public void ok(JSONObject response) {
                            Log.e("sss", "授权 成功");
                            account = ToJSonObject.jsonToBean(response.toString(), Account.class);
                            if (account.getLogin().equals(phoneNumber)) {
                                codeRESTFul getcodeRequest = new codeRESTFul();
                                getcodeRequest.getCode(RevisePassword.this, phoneNumber, new codeRESTFul.IgetCodeCb() {
                                    @Override
                                    public void ok(JSONArray response) {
                                        getCode.setEnabled(false);
                                        timecount.start();
                                        Gson gson = new Gson();
                                        List<String> list = gson.fromJson(response.toString(), new TypeToken<List<String>>() {
                                        }.getType());
                                        CODE = list.get(0);
                                        if (CODE == register_vercode.getText().toString()) {
                                            Log.e("ss", "获取code成功");
                                        }
                                    }

                                    @Override
                                    public void error(String error) {
                                        final MaterialDialog registerSuccess = new MaterialDialog.Builder(RevisePassword.this)
                                                .content("获取验证码失败！")
                                                .show();
                                        Handler handler = new Handler();
                                        handler.postDelayed(new Runnable() {
                                            public void run() {
                                                registerSuccess.dismiss();//设置弹框消失
                                            }
                                        }, 2000);
                                    }
                                });
                            }
                        }
                        @Override
                        public void error(String error) {
                            Log.e("eee","授权失败");
                        }
                    });
                }
            }
        });
        //下一步操作
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                register_vercode.getText().toString().equals(CODE)&& !CODE.equals("")
                if(register_vercode.getText().toString().equals(CODE)&& !CODE.equals("")){
                    phoneNumber=phone.getText().toString();
                    Intent intent = new Intent(RevisePassword.this, ResetPasswordActivity.class);
                    intent.putExtra("number", phoneNumber);
                    startActivity(intent);
                } else{
                    new MaterialDialog.Builder(RevisePassword.this).content("验证码输入错误，请重新输入").positiveText("确定").show();
                }
            }
        });
    }
    //计时内部类
    class timeCount extends CountDownTimer {
        public timeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {// 计时完毕
            getCode.setText("重新获取验证码");
            getCode.setEnabled(true);
            getCode.setTextColor(Color.parseColor("#333333"));
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            getCode.setEnabled(false);;//防止重复点击
            getCode.setText(millisUntilFinished / 1000 + "秒重新获取");
            getCode.setTextColor(Color.parseColor("#999999"));
        }
    }
}
